// pages/MyActivity/MyActivity.js
const db = wx.cloud.database()
const _ = db.command
const app = getApp()
let now = new Date().getTime()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    active: 1,
    endList: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onShow(options) {
    console.log("现在时间戳", now)
    this.getNotAdmitList()
    this.getHasAdmitList()
  },

  onLoad() {
    this.getEndList()
  },
  //获取未结束且未录取
  getNotAdmitList() {
    db.collection("project").where({
        endDateStamp: _.gt(now),
        vlnListNotAdmit: _.elemMatch({
          openid: _.eq(app.globalData.openid),
        })
      }).get()
      .then(res => {
        console.log("获取未结束且未录取的项目成功", res)
        this.setData({
          notAdmitList: res.data
        })
      }).catch(err => {
        console.log("获取未结束且未录取的项目失败", err)
      })
  },

  //获取未结束且已录取的项目
  getHasAdmitList() {
    db.collection("project").where({
        endDateStamp: _.gt(now),
        vlnListHasAdmit: _.elemMatch({
          vln_openid: _.eq(app.globalData.openid),
        })
      }).get()
      .then(res => {
        console.log("获取未结束且已录取的项目成功", res)
        this.setData({
          hasAdmitList: res.data
        })
      }).catch(err => {
        console.log("获取未结束且已录取的项目失败", err)
      })
  },

  //获取已结束的项目
  getEndList() {
    db.collection("project").where({
        endDateStamp: _.lt(now),
        vlnListHasAdmit: _.elemMatch({
          vln_openid: _.eq(app.globalData.openid),
        })
      }).get()
      .then(res => {
        console.log("获取截止项目1成功", res)
        this.setData({
          endList: this.data.endList.concat(res.data)
        })

        db.collection("project").where({
            endDateStamp: _.lt(now),
            vlnListNotAdmit: _.elemMatch({
              openid: _.eq(app.globalData.openid),
            })
          }).get()
          .then(res => {
            console.log("获取截止项目2成功", res)
            this.setData({
              endList: this.data.endList.concat(res.data)
            })
          }).catch(err => {
            console.log("获取截止项目2失败", err)
          })

      }).catch(err => {
        console.log("获取截止项目1失败", err)
      })
  },
  goProjectDetail(e) {
    console.log(e.currentTarget.dataset.id)
    wx.navigateTo({
      url: '/pages/project_all/projectDetail/projectDetail?id=' + e.currentTarget.dataset.id, //跟wxml里的data-id联动
    })
  },
})